<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/backStageBase.css}">
    <style>
        .layui-table img{
            max-width: 200px !important;
        }
        .layui-table-cell {
            height: auto !important;

        }
    </style>
</head>
<body class="layui-layout-body">
<div id="backstage" class="layui-layout layui-layout-admin">
    <div class="layui-header layui-bg-blue" >
        <div class="layui-logo">青木良品</div>
        <ul class="layui-nav  layui-layout-left" lay-filter="">
            <li class="layui-nav-item">商品管理</li>
        </ul>
        <ul class="layui-nav layui-layout-right layui-bg-blue">
            <li class="layui-nav-item" th:text="'欢迎你，'+${session.user.name}">
            </li>
            <li class="layui-nav-item"><a href="/mall/user/logout.do">退出</a></li>
        </ul>
    </div>

    <div class="layui-side layui-bg-gray">
        <ul class="layui-nav layui-nav-tree layui-bg-blue" lay-filter="test">
            <!-- 侧边导航: <ul class="layui-nav layui-nav-tree layui-nav-side"> -->
            <li class="layui-nav-item layui-nav-itemed"><a th:href="@{/admin/product/toList.html}">商品管理</a></li>
            <li class="layui-nav-item"><a th:href="@{/admin/activity/toList.html}">活动管理</a></li>
            <li class="layui-nav-item"><a th:href="@{/admin/order/toList.html}">订单管理</a></li>
            <li class="layui-nav-item"><a th:href="@{/admin/user/toList.html}">用户管理</a></li>
        </ul>
    </div>
    <div class="layui-body" style="overflow: hidden;height: 650px">
        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
                <li class="layui-this">商品信息</li>
                <li>添加商品</li>
            </ul>
            <div class="layui-tab-content"  style="height: 650px;">
                <div class="layui-tab-item layui-show">
                    <table id="productList"></table>
                </div>
                <div class="layui-tab-item">
                    <form id="addform" class="layui-form" enctype="multipart/form-data" method="post" action="add.do">
                        <div class="layui-form-item" style="display: none">
                            <div class="layui-input-block" style="width: 300px;">
                                <input type="text" name="shopId" class="layui-input" value="1">
                                <input type="text" name="sales" class="layui-input" value="0">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品标题</label>
                            <div class="layui-input-block" style="width: 300px;">
                                <input type="text" name="title" required  lay-verify="required" placeholder="请输入商品标题" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-block" style="width: 300px;">
                                <input type="text" name="name" required  lay-verify="required" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品分类</label>
                            <div class="layui-input-block" style="width: 200px;">
                                <select name="type" lay-verify="required">
                                    <option th:each="type:${session.typelist}" th:object="${type}" th:value="*{type}" th:text="*{remark}">类别名称</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品活动</label>
                            <div class="layui-input-block" style="width: 200px;">
                                <select name="activityId" lay-verify="required">
                                    <option th:each="activity:${session.activitylist}" th:object="${activity}" th:value="*{id}" th:text="*{activity}">类别名称</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商品价格</label>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <input type="text" name="price" placeholder="￥" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">库存</label>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <input type="text" name="store" placeholder="库存数量" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">商品信息</label>
                            <div class="layui-input-block" style="width: 400px;">
                                <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品图片</label>
                            <div class="layui-input-block">
                                <input id="image1" type="file" name="image1" accept="image/*" lay-verify="required|image1"><br>
                                <input id="image2" type="file" name="image2" accept="image/*" lay-verify="required|image2"><br>
                                <input id="image3" type="file" name="image3" accept="image/*" lay-verify="required|image3"><br>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="layui-btn" lay-submit="">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:src="@{/js/jqurey.js}"></script>
<script th:src="@{/layui/layui.js}"></script>
<script type="text/javascript">
    var pageIndex=1
    layui.use(['element','form','table','layer'], function(){
        var element = layui.element;
        var form = layui.form;
        var layer = layui.layer;
        // 表格渲染
        var table = layui.table
        form.verify({
            image1:function(value,item){
                if($('input[name=image1]').val() == '')
                    return '请选择主图片';
            },
            image2:function(value,item){
                if($('input[name=image2]').val() == '')
                    return '请选择副图片';
            },
            image3:function(value,item){
                if($('input[name=image2]').val() == '')
                    return '请选择副图片';
            }
        });
        table.render({
            elem: '#productList',
            height: 600,
            cols: [ [
                {field: 'id', title: 'ID', width: 60, sort:true},
                {field: 'mainImage', title: '商品图片', width: 230, templet:function(d){return '<div id="imageShow" style="display: inline-block"><img src="/mall/image/'+d.mainImage+'" width="160px" height="160px"></div>'}},
                {field: 'name', title: '商品名称', width: 230},
                {field: 'state', title: '类别', width: 110, templet:function(d){
                        if(d.type=='1'){return '<span>木制</span>'}
                        else if(d.type=='2'){return '<span>不锈钢</span>'}
                        else if(d.type=='3'){return '<span>陶瓷</span>'}
                        else if(d.type=='4'){return '<span>塑料</span>'}}},
                {field: 'price', title: '商品价格', width: 110, sort: true},
                {field: 'store', title: '库存', width: 110, sort: true},
                {field: 'sales', title: '销量', width: 110, sort: true},
                {field: 'state', title: '状态', width: 110, templet:function(d){if(d.string1=='0'){return '<span>下架中</span>'}else if(d.string1=='1'){return '<span>上架中</span>'}}},
                {field: 'operate', title: '操作', width: 190, templet:function(d){
                        if(d.string1=='0'){
                            return '<a href="/mall/admin/product/toEdit.html?id='+d.id+'"><button class="layui-btn layui-btn-sm">修改</button></a>' +
                                '<button class="layui-btn layui-btn-normal layui-btn-sm" onclick="sellOn('+d.id+')">上架</button>' +
                                '<button class="layui-btn layui-btn-danger layui-btn-sm" onclick="delProduct('+d.id+')">删除</button>'
                        }else if(d.string1=='1'){
                            return '<a href="/mall/admin/product/toEdit.html?id='+d.id+'"><button class="layui-btn layui-btn-sm">修改</button></a>' +
                                '<button class="layui-btn layui-btn-warm layui-btn-sm" onclick="sellout('+d.id+')">下架</button>' +
                                '<button class="layui-btn layui-btn-danger layui-btn-sm" onclick="delProduct('+d.id+')">删除</button>'
                        }
                    }}
            ]],
            url: 'list.do',
            // parseData: function (res) {
            //     return{
            //         "msg": res.message, //解析提示文本
            //         "data": res.data, //解析数据列表
            //         "count": res.data.length, //解析数据长度
            //     }
            // }
            response: {
                statusName: 'state' //规定数据状态的字段名称，默认：code
                ,statusCode: 0 //规定成功的状态码，默认：0
                ,msgName: 'message' //规定状态信息的字段名称，默认：msg
                ,dataName: 'data' //规定数据列表的字段名称，默认：data
            }
        })
        layer.photos({
            photos: '#imageShow'
            ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
        });
    });


    $(function () {
        //getData(1)
        getData2()
    })
    function getData(pageindex) {
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "list.do",
            //请求成功
            success : function(data) {

            },
            error : function (e) {

            }
        });
    }
    function delProduct(id) {
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "del.do?id="+id,
            //请求成功
            success : function(data) {
                alert("删除成功")
                location.reload();
            },
            error : function (e) {

            }
        });
    }
    function getData2() {
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "getTotal",
            //请求成功
            success : function(data) {

            },
            error : function (e) {

            }
        });
    }
    // 上架
    function sellOn(id) {
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "soldOn.do?id="+id,
            //请求成功
            success : function(data) {
                alert("上架成功")
                location.reload()
            },
            error : function (e) {
            }
        });
    }
    // 下架
    function sellout(id) {
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "soldOut.do?id="+id,
            //请求成功
            success : function(data) {
                alert("下架成功")
                location.reload()
            },
            error : function (e) {
            }
        });
    }
    //修改，更新
    function updata(id) {
        $.ajax({
            //请求方式
            type : "GET",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "toEdit.html?id="+id,
            //请求成功
            success : function(data) {
            },
            error : function (e) {
            }
        });
    }
    function addproduct() {
        var mydata = {};
        var image=[];
        var t = $('#addform').serializeArray();
        console.log(t)
        var img1=$('#image1').val()
        var img2=$('#image2').val()
        var img3=$('#image3').val()
        if(img1==''){
            alert("请选择商品主图")
            return
        }else{
            image.push(img1)
        }
        if(img2!=''){
            image.push(img2)
        }
        if(img3!=''){
            image.push(img3)
        }
        t['image']=image

        $.ajax({
            //请求方式
            type : "POST",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            //请求地址
            url : "add.do",
            // 数据
            data: JSON.stringify(t),
            dataType: "JSON",
            //请求成功
            success : function(data) {
                location.reload()
            },
            error : function (e) {

            }
        });
    }
</script>
</html>